package com.kgc.skconsumer.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.collection.pojo.SalesAdvance;
import com.kgc.skconsumer.feign.CwWorkFlowFeign;
import com.kgc.skconsumer.feign.SalesAdvanceFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/SalesAdvanceController")
public class SalesAdvanceController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private SalesAdvanceFeign salesAdvanceFeign;

    @Autowired
    private CwWorkFlowFeign cwWorkFlowFeign;

    @RequestMapping("/selectSalesAdvanceByPage")
    public ResponseMsg selectSalesAdvanceByPage(@RequestBody Map<String, Object> map) {
        logger.info("SalesAdvanceController  selectSalesAdvanceByPage params is {}", map.toString());
        ResponseMsg responseMsg = salesAdvanceFeign.selectSalesAdvanceByPage(map);
        logger.debug("SalesAdvanceController  selectSalesAdvanceByPage responseMsg is {}", responseMsg);
        return responseMsg;
    }

    @RequestMapping("/updateSalesAdvance")
    public  ResponseMsg  updateSalesAdvance(@RequestBody Map<String, Object> map) {
        logger.info("SalesAdvanceController  updateSalesAdvance params is {}", map.toString());
        ResponseMsg responseMsg = salesAdvanceFeign.updateSalesAdvance(map);
        logger.debug("SalesAdvanceController  updateSalesAdvance responseMsg is {}", responseMsg);
        return responseMsg;
    }

    @RequestMapping("/insertSalesAdvance")
    public  ResponseMsg insertSalesAdvance (@RequestBody Map<String, Object> map) {
        logger.info("SalesAdvanceController  insertSalesAdvance params is {}", map.toString());
        ResponseMsg responseMsg = salesAdvanceFeign.insertSalesAdvance(map);
        ObjectMapper mapper = new ObjectMapper();
        SalesAdvance salesAdvance = mapper.convertValue(map.get("salesAdvance"), SalesAdvance.class);
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",salesAdvance.getDocCode());//单据编号
        variables.put("type",salesAdvance.getTypeId());//流程类型
        //创建一个流程实例
        cwWorkFlowFeign.startProcess(variables);
        logger.debug("SalesAdvanceController  insertSalesAdvance responseMsg is {}", responseMsg);
        return responseMsg;
    }

    @RequestMapping("/deleteSalesAdvance")
    public  ResponseMsg deleteSalesAdvance(@RequestBody Map<String, Object> map) {
        logger.info("SalesAdvanceController  deleteSalesAdvance params is {}", map.toString());
        ResponseMsg responseMsg = salesAdvanceFeign.deleteSalesAdvance(map);
        logger.debug("SalesAdvanceController  deleteSalesAdvance responseMsg is {}", responseMsg);
        return responseMsg;
    }

    @RequestMapping("/checkSalesAdvance")
    public  ResponseMsg checkSalesAdvance(@RequestBody Map<String, Object> map) {
        logger.info("SalesAdvanceController  checkSalesAdvance params is {}", map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",(String) map.get("docCode"));//单据编号
        variables.put("checkManId",(Integer) map.get("checkManId"));//审批人
        variables.put("checkManName",(String) map.get("checkManName"));//审批人
        variables.put("checkComment",(String) map.get("checkComment"));//审批意见
        variables.put("checkDate",new Date());//审批时间
        variables.put("checkRes",(boolean)map.get("checkRes"));//审批结果
        com.kgc.activiti.util.ResponseMsg msg=  cwWorkFlowFeign.competeTask(variables);
        responseMsg.setCode(msg.getCode());
        responseMsg.setMsg(msg.getMsg());
        logger.debug("SalesAdvanceController  checkSalesAdvance responseMsg is {}", responseMsg);
        return responseMsg;
    }

    @RequestMapping("/selectSalesAdvanceByCode")
    public  ResponseMsg selectSalesAdvanceByCode(@RequestBody Map<String, Object> map) {
        logger.info("SalesAdvanceController  selectSalesAdvanceByCode params is {}", map.toString());
        ResponseMsg responseMsg = salesAdvanceFeign.selectSalesAdvanceByCode(map);
        logger.debug("SalesAdvanceController  selectSalesAdvanceByCode responseMsg is {}", responseMsg);
        return responseMsg;
    }

}
